package grapid;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Grapid公共类
 * 
 * @author grapid
 * @version 2.0
 * 
 */
public class Grapid {

	/**
	 * 数据库类型 - MSSQL
	 */
	public static final Integer DB_MSSQL = 1;
	/**
	 * 数据库类型 - MYSQL
	 */
	public static final Integer DB_MYSQL = 2;
	/**
	 * 数据库类型 - SQLITE
	 */
	public static final Integer DB_SQLITE = 3;
	// public static final Integer DB_ORACLE = 4;

	/**
	 * 数据库驱动 - MSSQL
	 */
	public static final String DB_DRIVER_MSSQL = "net.sourceforge.jtds.jdbc.Driver";
	/**
	 * 数据库驱动 - MYSQL
	 */
	public static final String DB_DRIVER_MYSQL = "com.mysql.jdbc.Driver";
	/**
	 * 数据库驱动 - SQLITE
	 */
	public static final String DB_DRIVER_SQLITE = "org.sqlite.JDBC";

	/**
	 * 数据库连接URL例子 - MSSQL, "jdbc:jtds:sqlserver://localhost:1433/dbname"
	 */
	public static final String DB_CONNECTION_URL_MSSQL_DEMO = "jdbc:jtds:sqlserver://localhost:1433/dbname";
	/**
	 * 数据库连接URL例子 - MYSQL,
	 * "jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true"
	 */
	public static final String DB_CONNECTION_URL_MYSQL_DEMO = "jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true";
	/**
	 * 数据库连接URL例子 - SQLITE, "jdbc:sqlite:filepath\\database.db"
	 */
	public static final String DB_CONNECTION_URL_SQLITE_DEMO = "jdbc:sqlite:filepath\\database.db";

	/**
	 * GQuery工作模式 - 启用调试
	 */
	public static final Integer GQuery_MODE_DEBUG = 1;
	/**
	 * GQuery工作模式 - 关闭字符串自动转义
	 */
	public static final Integer GQuery_MODE_DATA_FILTER = 2;
	/**
	 * GQuery工作模式 - 启用条件null值过滤(仅用于GQueryP)
	 */
	public static final Integer GQuery_MODE_CONDITION_NULL = 4;

	/**
	 * GJSON相关定义 - 页面输出编码
	 */
	public static String DEFAULT_GJSON_CHARACTERENCODING = "utf-8";
	/**
	 * GJSON相关定义 - jQuery跨域请求回调函数名
	 */
	public static String DEFAULT_GJSON_JSONCALLBACKMETHODNAME = "jsoncallback";

	/**
	 * Grapid默认数据库类型
	 */
	public static Integer DEFAULT_DBTYPE = 1;
	/**
	 * GQuery默认工作模式
	 */
	public static Integer DEFAULT_GQUERY_MODE = 0;

	/**
	 * 初始化
	 */
	public Grapid() {
		Properties properties = new Properties();
		InputStream in = null;
		try {
			in = getClass().getResourceAsStream("/grapid.properties");
			properties.load(in);
			Enumeration<?> keys = properties.propertyNames();
			System.out.println("Loading Grapid.properties Start.");
			while (keys.hasMoreElements()) {
				String key = (String) keys.nextElement();
				String value = properties.getProperty(key);
				value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
				properties.setProperty(key, value);
				System.out.println("> " + key + " = " + value);
			}
			try {
				DEFAULT_DBTYPE = Integer.parseInt(properties.getProperty("DEFAULT_DBTYPE", "1"));
			} catch (Exception e) {
			}
			try {
				DEFAULT_GQUERY_MODE = Integer.parseInt(properties.getProperty("DEFAULT_GQUERY_MODE", "0"));
			} catch (Exception e) {
			}
			DEFAULT_GJSON_CHARACTERENCODING = properties.getProperty("DEFAULT_GJSON_CHARACTERENCODING", "utf-8");
			DEFAULT_GJSON_JSONCALLBACKMETHODNAME = properties.getProperty("DEFAULT_GJSON_JSONCALLBACKMETHODNAME",
					"jsoncallback");
			System.out.println("Loading Grapid.properties End.");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException ex) {
			}
		}
	}
}
